package vim.meet.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * 配置：
 * <aop:aspectj-autoproxy/>  
 * 2016年11月19日
 *
 */
@Component
@Aspect
public class ResponseTimeAspectAdvice {
	
    private Logger logger = LoggerFactory.getLogger(ResponseTimeAspectAdvice.class);

    @Around("execution(* cn.freemethod.service.*.*(..))")
    public Object doAround(ProceedingJoinPoint jp) throws Throwable {
    	System.out.println("in"+this.getClass().getName());
        MethodSignature joinPointObject = (MethodSignature) jp.getSignature();
        Method method = joinPointObject.getMethod();
        String className = jp.getSignature().getDeclaringType().getName();
        String methodName = method.getName();
        Object[] args = jp.getArgs();
        StringBuffer buffer = new StringBuffer("");
        for (Object object : args) {
            buffer.append(String.valueOf(object)).append(",");
        }
        Long start = System.currentTimeMillis();
        try {
            return jp.proceed();
        } finally {
            long costTime = System.currentTimeMillis() - start;
            if (logger.isDebugEnabled()) {
                logger.debug("do methodName over ,className={},methodName={},args={},costTime={}",
                        className, methodName, buffer.toString(), costTime);
            } else if (costTime > 400) {
                logger.error("do methodName over ,className={},methodName={},args={},costTime={}",
                        className, methodName, buffer.toString(), costTime);
            }
        }
    }
}